﻿#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: TestEmailSender.cs
//

#endregion

using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

namespace Novagoal.Utils.Implementations
{
    public class TestEmailSender : IEmailSender
    {
		private readonly MailCredential _defaultCredential;

		private readonly string _destiny;

        /// <summary>
        /// Initializes a new instance of the <see cref="TestEmailSender"/> class.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="account">The account.</param>
        /// <param name="password">The password.</param>
        /// <param name="usesSsl">if set to <c>true</c> [uses SSL].</param>
        /// <param name="destiny">The destiny.</param>
		public TestEmailSender(string server, string account, string password, bool usesSsl, string destiny)
		{
			_defaultCredential = new MailCredential
			                     	{
			                     		Server = server,
			                     		Account = account,
			                     		Password = password,
			                     	};
			UsesSsl = usesSsl;
			_destiny = destiny;
		}

        /// <summary>
        /// Gets or sets a value indicating whether [uses SSL].
        /// </summary>
        /// <value><c>true</c> if [uses SSL]; otherwise, <c>false</c>.</value>
		public bool UsesSsl { get; set; }

        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="givenCredential">The given credential.</param>
        /// <param name="toEmail"> A <c>System.String</c> that contains the addresses of the recipients of the e-mail message.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="html">The HTML as a string.</param>
		public void SendEmail(MailCredential givenCredential, string toEmail, string subject, string html)
		{
			SendEmail(givenCredential, toEmail, subject, html, Enumerable.Empty<string>());
		}

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="givenCredential">The given <see cref="MailCredential"/> credential.</param>
        /// <param name="toEmail"> A <c>System.String</c> that contains the addresses of the recipients of the e-mail message.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="html">The HTML as string.</param>
        /// <param name="attachments">The attachment collection.</param>
		public void SendEmail(MailCredential givenCredential, string toEmail, string subject, string html, IEnumerable<string> attachments)
		{
			var credential = _defaultCredential;

            using (var msg = GetMailMessage(credential, toEmail, subject, html))
            {
                foreach (var attach in attachments)
                {
                    var attached = new Attachment(attach, MediaTypeNames.Application.Octet);
                    msg.Attachments.Add(attached);
                }
                var client = new SmtpClient
                                 {
                                     Credentials =
                                         new System.Net.NetworkCredential(credential.Account, credential.Password),
                                     Host = credential.Server,
                                     EnableSsl = UsesSsl,
                                 };
                client.Send(msg);
            }
		}

        public void SendEmail(MailCredential givenCredential, string toEmails, string carbonCopyEmails, string blindCarbonCopyEmails, string subject, string html, IEnumerable<string> attachments)
        {
            SendEmail(givenCredential, toEmails, subject, html, attachments);
        }

        /// <summary>
        /// Sends an email.
        /// </summary>
        /// <param name="givenCredential">The given <see cref="MailCredential"/> credential.</param>
        /// <param name="emailTask">The given <see cref="EmailTask"/> object with all email properties.</param>
        public void SendEmail(MailCredential givenCredential, EmailTask emailTask)
        {
            SendEmail(givenCredential,emailTask.ToEmail, emailTask.Subject, emailTask.Html, emailTask.Attachments);
        }

        /// <summary>
        /// Gets the mail message.
        /// </summary>
        /// <param name="credential">The credential.</param>
        /// <param name="toEmail"> A <c>System.String</c> that contains the addresses of the recipients of the e-mail message.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="html">The HTML as a string.</param>
        /// <returns>A <see cref="MailMessage"/></returns>
        private MailMessage GetMailMessage(MailCredential credential, string toEmail, string subject, string html)
        {
            return new MailMessage(credential.Account, _destiny)
            {
                Subject = string.Format("[Test] {0} -> {1}", subject, toEmail),
                SubjectEncoding = Encoding.UTF8,
                Body = html,
                BodyEncoding = Encoding.UTF8,
                IsBodyHtml = true,
                Priority = MailPriority.Normal,
            };
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
		public void Dispose()
		{
			//do nothing
		}
    }
}
